/**
 * Created by zhangwx on 2015/11/16.
 */


/**
 *
 * @param data 数据
 * @param ops  配置
 */
function initData(data,ops){
    source.clear();
    if(heatMap){
        map.removeLayer(heatMap);
    }
   if(pointsMap){
       map.removeLayer(pointsMap);
   }
    removelistener();
//数据图层要初始化
    var defaultOptions={
        type:"group",
        data:"data",
        lonb:"lng",
        latb:"lat",
        displayField:[],
        userInfo:"userInfo",
        cityCode:"cityName",
        max:100,//值
        count:"count"
    };
    var options = $.extend(defaultOptions,ops);
    if(data){
        if(options.type=="groupMap"){

            for(var i=0;i<data[options.data].length;i++){
                source.addFeature(addPointFeature({
                    lon:parseFloat(data[options.data][i][options.lonb]),
                    lat:parseFloat(data[options.data][i][options.latb]),
                    weight:data[options.data][i][options.count]
                }));

            }

           heatMap.setSource(source);
            map.addLayer(heatMap);

            var extent = heatMap.getSource().getExtent();
            map.getView().fit(extent, map.getSize());
            map.getView().setZoom(map.getView().getZoom()-1);

        }else{

            for(var i=0;i<data[options.data].length;i++){
                var content ="";
                for(var f in options.displayField){
                    content+=options.displayField[f].label+":"+data[options.data][i][options.displayField[f].value]+'\n';
                }

                source.addFeature(addPointFeature({
                    lon:parseFloat(data[options.data][i][options.lonb]),
                    lat:parseFloat(data[options.data][i][options.latb]),
                    content:content
                }));


            }

           pointsMap.setSource(source);
            map.addLayer(pointsMap);
            var extent = pointsMap.getSource().getExtent();
            map.getView().fit(extent, map.getSize());
            map.getView().setZoom(map.getView().getZoom()-1);
            addlistener();
        }

    }
}

function removelistener(){
    if (select !== null) {
        map.removeInteraction(select);
    }

}
function addlistener(){

    select = new ol.interaction.Select({
        condition: ol.events.condition.click
    });
    map.addInteraction(select);
   select.on('select', function(e) {
       var feature=e.selected;
        if(feature.length>0) {
            $(element).popover('hide');
            popup.setPosition(feature[0].getGeometry().getCoordinates());
            $(element).popover({
                trigger: 'manual',
                placement: 'top', //placement of the popover. also can use top, bottom, left or right
                html: 'true', //needed to show html of course
                content: '<div style="background:rgba(0,0,0,0.1);width:245px;height:30px;">' + feature[0].get('title') + '</div>',
                animation: true
            }).popover('show').on("mouseenter", function () {
                var _this = this;
                $(this).popover("show");
                $(this).siblings(".popover").on("mouseleave", function () {

                    setTimeout(function () {
                        if (!$(".popover:hover").length) {
                            $(_this).popover("hide")
                        }
                    }, 2000);
                });
            }).on("mouseleave", function () {
                var _this = this;
                setTimeout(function () {
                    if (!$(".popover:hover").length) {
                        $(_this).popover("hide")
                    }
                }, 2000);
            });
        }
   });

}
/**
 *
 * @param options
 *         lon
 *         lat
 *         content
 *         weight
 *
 */
function addPointFeature(options){
    var feature= new ol.Feature(
        new ol.geom.Point(
            ol.proj.transform(
                [options.lon, options.lat],
                'EPSG:4326',
                'EPSG:3857'
            )
        )
    )
    if(options.weight){
        feature.set("weight",options.weight);

    }
    if(options.content){
        feature.set("title",options.content);
        feature.setStyle(pointStyle);


    }

   return feature;
}

